# 长字符串 原始字符串 普通字符串 字符串拼接
long_text = """longtext"""
row_text = r'row_text \r\n'
normal_text = 'normal text'
concat_text = "concat" "text" "."

print(long_text)
print(row_text)
print(normal_text)
print(concat_text)

# unicode字符
unicode_web_site = "http://unicode-table.com"
u1 = "\u00C6"
print(u1, type(u1))  # Æ <class 'str'>
print("\U0001F60A")  # 😊
print("this is cat: \N{cat}")  # this is cat: 🐈
print("this is bug: \N{bug}")  # this is bug: 🐛
print("this is Cat Face: \N{Cat Face}")  # this is Cat Face: 🐱
#

# 不可变bytes 可变bytearray
b1 = b'Hello'
print(b1)  # b'Hello'

# 字符串的编码成bytes
acsii_source_encode = "Hello world".encode('ASCII')
print(acsii_source_encode, type(acsii_source_encode), len(acsii_source_encode))  # b'Hello world' <class 'bytes'> 11

utf8_source_encode = "Hello world".encode('utf8')
print(utf8_source_encode, len(utf8_source_encode))  # b'Hello world' 11

utf32_source_encode = "Hello world".encode('utf32')
print(utf32_source_encode,
      len(utf32_source_encode))  # b'\xff\xfe\x00\x00H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00 \x00\x00\x00w\x00\x00\x00o\x00\x00\x00r\x00\x00\x00l\x00\x00\x00d\x00\x00\x00' 48

#
# 字符串转码时遇到无法处理的字符的处理策略
# print("abc中嗯".encode('ASCII', "strict"))  #  UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
print("abc中嗯".encode('ASCII', "ignore"))  # b'abc' 忽略无法处理的字符
print("abc中嗯".encode('ASCII', "replace"))  # b'abc??' 替换成问号
print("abc中嗯".encode('ASCII', "backslashreplace"))  # b'abc\\u4e2d\\u55ef' 按照unicode编码
print("abc中嗯".encode('ASCII', "xmlcharrefreplace"))  # b'abc&#20013;&#21999;' 按照html编码

# 默认编码也是UTF-8
print("Hællå, wørld!".encode())  # b'H\xc3\xa6ll\xc3\xa5, w\xc3\xb8rld!'
print("Hællå, wørld!".encode('utf8'))  # b'H\xc3\xa6ll\xc3\xa5, w\xc3\xb8rld!'
print(b'H\xc3\xa6ll\xc3\xa5, w\xc3\xb8rld!'.decode())
print(b'H\xc3\xa6ll\xc3\xa5, w\xc3\xb8rld!'.decode('utf8'))

# 使用bytes函数编码
b1 = bytes("Hællå, wørld!", encoding="utf-8")
print(b1, type(b1), len(b1))  # b'H\xc3\xa6ll\xc3\xa5, w\xc3\xb8rld!' <class 'bytes'> 16

# 使用str函数解码
print(str(b'H\xc3\xa6ll\xc3\xa5, w\xc3\xb8rld!', 'utf8'))  # Hællå, wørld!

# 源码编码指定编码格式
# _*_ coding: utf-8 _*_

#
x = bytearray(b'Hello')
x[1] = ord(b'u')
print(x)  # bytearray(b'Hullo')

print(ord(b'u'))  # 117
print(chr(117))  # u
